<?php

namespace summer\swoole\pool;

use summer\swoole\pool\contract\ConnectionInterface;
use summer\swoole\pool\utils\ContextUtil;

class Context
{
    /**
     * @var string
     */
    protected $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    /**
     * Get a connection from request context.
     */
    public function connection(): ?ConnectionInterface
    {
        if (ContextUtil::has($this->name)) {
            return ContextUtil::get($this->name);
        }

        return null;
    }

    public function set(ConnectionInterface $connection): void
    {
        ContextUtil::set($this->name, $connection);
    }
}